package com.wtgroup.demo.mianshi.jvm.GC;

import java.util.concurrent.TimeUnit;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/3/16 19:19
 */
public class D2_DeadLock {
    /**
     * 一个线程持有锁, 其他线程等待
     *
     */

    public static void main(String[] args) {
        Object lock = new Object();

        new Thread(() -> {
            synchronized (lock) {
                int i = 0;
                while (i++ < 1000) {
                    System.out.println(Thread.currentThread().getName() + " 村霸运行第 " + i + " 次");
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "cunba").start();

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(() -> {
                synchronized (lock) {
                    System.out.println(finalI);
                }
            }).start();
        }
    }

}
